package com.csg.ses.controller;

import com.csg.ses.common.annotation.LoginToken;
import com.csg.ses.entity.ses.SysRolesEntity;
import com.csg.ses.service.ses.SysRoleRightService;
import com.csg.ses.service.ses.SysRolesService;
import com.csg.ses.vo.common.CascadeDropData;
import com.csg.ses.vo.common.IdListVo;
import com.csg.ses.vo.common.Result;
import com.csg.ses.vo.common.ResultStatus;
import com.csg.ses.vo.list.RoleVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色信息
 * @author wangjun
 * @date 2020/11/16
 */
@RestController
@RequestMapping("/roles")
@Api(tags = "角色信息接口")
public class SysRolesController {
    @Autowired
    private SysRolesService sysRolesService;

    @Autowired
    private SysRoleRightService sysRoleRightService;

    /**
     * 列表
     */
    @ApiOperation(value = "角色信息")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "role", value = "角色信息", dataType = "SysRolesEntity", paramType = "body"),
        @ApiImplicitParam(name = "current", value = "当前页", required = true ,dataType = "Integer"),
        @ApiImplicitParam(name = "size", value = "每页记录数", required = true ,dataType = "Integer")
    })
    @GetMapping("/list")
    @LoginToken
    public Result<List<SysRolesEntity>> list(SysRolesEntity role, Long current, Long size) {
        return sysRolesService.retrieve(role, current, size);
    }

    /**
     * 角色下拉数据
     * @return
     */
    @ApiOperation(value = "角色下拉数据")
    @GetMapping("/drop-data")
    @LoginToken
    public Result dropData() {
        return sysRolesService.dropData();
    }

    @ApiOperation(value = "角色级联下拉")
    @GetMapping("/cascade-drop")
    @LoginToken
    public Result<List<CascadeDropData>> cascadeDrop() {
        return sysRolesService.cascadeDrop();
    }

    /**
     * 根据当前登录人id获取角色信息
     */
    @ApiOperation(value = "根据当前登录人id获取角色信息")
    @ApiImplicitParam(name = "loginId", value = "角色控制对象id", dataType = "String", required = true)
    @GetMapping("/login/{loginId}")
    @LoginToken
    public Result getUserRoles(@PathVariable String loginId) {
        try {
            return new Result(sysRolesService.getUserRoles(loginId), ResultStatus.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultStatus.FAILED);
        }
    }

    /**
     * 删除角色信息
     * @param idList
     * @return
     */
    @ApiOperation(value = "删除角色信息")
    @ApiImplicitParam(name = "idList", value = "角色信息ID数组", dataType = "IdListVo", paramType = "body", required = true)
    @DeleteMapping("/del")
    @LoginToken
    public Result del(@RequestBody IdListVo idList) {
        sysRoleRightService.deleteByroleId(idList);
        return sysRolesService.del(idList);
    }

    @ApiOperation(value = "根据id查找角色信息")
    @ApiImplicitParam(name = "id", value = "角色信息id", dataType = "String", required = true)
    @GetMapping("/{id}")
    @LoginToken
    public Result<SysRolesEntity> getById(@PathVariable String id) {
        return sysRolesService.getById(id);
    }

    /**
     * 保存角色信息
     * @param
     * @return
     */
    @ApiOperation(value = "保存保存角色信息")
    @ApiImplicitParam(name = "roleVo", value = "角色类型数组", dataType = "RoleVo", paramType = "body", required = true)
    @PostMapping("/save")
    @LoginToken
    public Result<String> save(@RequestBody RoleVo roleVo) {
        return new Result(sysRolesService.save(roleVo), ResultStatus.SUCCESS);
    }

    @PostMapping("/addRole")
    @LoginToken
    public Result<String> addRole(@RequestBody SysRolesEntity sysRolesEntity) {
        boolean result = sysRolesService.saveOredit(sysRolesEntity);
        if (result) {
            return new Result("保存成功！", ResultStatus.SUCCESS);
        } else {
            return new Result("保存失败！", ResultStatus.FAILED);
        }
    }
}